<?php

namespace App\Service\Activity;

use App\Helpers\AliyunUtil;
use App\Model\Act\InviteRewardLog;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 邀请有奖页面-奖励排行榜
 */
class RewardRankService extends BaseService
{
    public function handle()
    {
        $ret['list'] = [];
        $rankList = InviteRewardLog::getRank(10);
        $userIds = array_keys($rankList);
        $userProfiles = UserProfile::getUsersProfile($userIds);
        foreach ($userProfiles as $profile) {
            $coin = $rankList[$profile['user_id']] ? round($rankList[$profile['user_id']], 1) : 0;
            $profiles[$profile['user_id']] = [
                'user_id' => $profile['user_id'],
                'nick_name' => mb_substr($profile['nick_name'], 0, 2) . "***",
                'profile_id' => 'ID10****' . mb_substr($profile['user_profile_id'], -1),
                'avatar' => UserProfile::buildAvatar($profile['avatar'], 80),
                'desc' => "累积获得{$rankList[$profile['user_id']]}金币",
                'reward' => $coin,
            ];
        }

        $rank = 1;
        foreach ($rankList as $userId => $coin) {
            array_push($ret['list'], array_merge($profiles[$userId], ['rank' => $rank++]));
        }
        return $ret;
    }
}
